package com.company.ecis.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.company.ecis.entity.BalanceInfo;
import com.company.ecis.entity.Userinfo;
import com.company.ecis.entity.vo.UserinfoSmallVO;
import com.company.ecis.entity.vo.UserinfoVO;
import com.company.ecis.mapper.BalanceInfoMapper;
import com.company.ecis.service.BalanceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.xml.internal.bind.v2.TODO;
import lombok.experimental.Accessors;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.TileObserver;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-04
 */
@Service
public class BalanceInfoServiceImpl extends ServiceImpl<BalanceInfoMapper, BalanceInfo> implements BalanceInfoService {

    @Autowired
    BalanceInfoMapper balanceInfoMapper;

    @Override
    public String getBalance(HttpSession session) {
        JSONObject jsonObject = JSONUtil.createObj();
        //获取用户信息
        Userinfo user = (Userinfo) session.getAttribute("余额用户信息");
        //TODO 判断该用户的状态是否正常

        //判断用户信息是否有内容
        if(Objects.isNull(user)){
            throw new RuntimeException("没有用户的信息");
        }

        //获取该用户的名字
        String username = user.getUsername();
        //查询对应的余额
        LambdaQueryWrapper<BalanceInfo> selectWrapper = new LambdaQueryWrapper<>();
        selectWrapper.eq(BalanceInfo::getUsername,username);
        BalanceInfo balanceInfo = balanceInfoMapper.selectOne(selectWrapper);

        //如果查询的信息为空
        if(Objects.isNull(balanceInfo)){
            throw new RuntimeException("查询不到相关信息");
        }

        //返回数据
        jsonObject.set("code",200).set("msg","查询成功").set("data",balanceInfo.getBalance());
        System.out.println(JSONUtil.toJsonStr(jsonObject));
        return JSONUtil.toJsonStr(jsonObject);
    }

    @Override
    public void updateBalanceByUsername(BalanceInfo balanceInfo) {
        QueryWrapper<BalanceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",balanceInfo.getUsername());
        BalanceInfo temp = balanceInfoMapper.selectOne(queryWrapper);
        UpdateWrapper<BalanceInfo> wrapper = new UpdateWrapper<>();
        temp.setBalance(temp.getBalance().add(balanceInfo.getBalance()));
        wrapper.eq("username",balanceInfo.getUsername());
        balanceInfoMapper.update(temp,wrapper);
    }

    @Override
    public BalanceInfo getBalanceByUsername(String username) {
        QueryWrapper<BalanceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return balanceInfoMapper.selectOne(wrapper);
    }

    @Override
    public void insertBalance(BalanceInfo balanceInfo) {
        balanceInfoMapper.insert(balanceInfo);
    }
}
